//
//  CLHomeCarViewController.m
//  ZhuKuang
//
//  Created by jjkj on 17/7/12.
//  Copyright © 2017年 jjkj. All rights reserved.
//
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kAlarmViewHeight ((CGFloat)80/375) * kScreenWidth
#define kDefalutCarShopId @"a892c48707064e189b2173c703420364"
#define kDefaultWorkShopName @"汽车作业区"

#import "CLHomeCarViewController.h"
#import "HomeCarModel.h"
#import "FCSwitchView.h"
#import "FCAlamView.h"
#import "FCMileageAndOilWearView.h"
#import "CLWorkAreaDetailViewController.h"
#import "HomeCarModel.h"
#import "FCVehicleReamTimeStatusController.h"//车辆实时状态
#import "CustomChooseBoxView.h"
#import "YHIolInfoListView.h"
#import "UIButton+RoundedCorner.h"
#import "HomeCarModel.h"
#import "CarShopListModel.h"

#define bottomHeight 90
@interface CLHomeCarViewController ()

@property (nonatomic,strong) NSMutableDictionary *homeCarParamsDic;//请求参数

@property (nonatomic,strong) HomeCarModel *homeCarModel;//车辆首页model

@property (nonatomic,strong) NSMutableArray *carShopListArray;//车间数据源

@property (nonatomic,strong) CarShopListModel *seleetdCarShopModel;



@property (nonatomic,copy) NSString *carShopId;



/**报警列表*/
@property (nonatomic,copy) YHIolInfoListView * iolInfoListView;

@end

@implementation CLHomeCarViewController

-(YHIolInfoListView *)iolInfoListView
{
    if (!_iolInfoListView) {
        _iolInfoListView = [YHIolInfoListView viewFromXib];
        _iolInfoListView.frame = CGRectMake(5, kScreenHeight - bottomHeight, kScreenWidth-10, 0);
    }
    return _iolInfoListView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpTimeInfo];
    
    if (nil == self.seleetdCarShopModel) {
        self.carShopId = kDefalutCarShopId;
    }
    
    self.homeCarParamsDic = [NSMutableDictionary dictionaryWithDictionary:@{
                                                                            @"starttime":self.startTime,
                                                                            @"endtime":self.endTime,
                                                                            
                                                 @"workShopId":self.carShopId,
                                                                            
                            }];
    [self getHomeCarInfoListWith:self.homeCarParamsDic];
    [self getShopCarListInfoWith:nil];
    [self setUpChooseWorkAreaBoxView];
    [self setUpChooseBoxView];
    [self setUpAlarmView];
    [self setUpVehicleAmountAndMileageView];
    // Do any additional setup after loading the view.
}
- (void)setUpChooseBoxView {
    [self.chooseBoxBg changeChooseBoxTitle:@"汽车作业区"];
}
- (void)setUpTimeInfo {
    
    NSString *todayDate = [CommClass getTodayDateStr];
    
    if (nil == self.startTime) {
        self.startTime = todayDate;
    }
    
    if (nil == self.endTime) {
        self.endTime = todayDate;
    }
    
}

#pragma mark - 时间选择处理


- (void)setUpAlarmView {
    
    __weak typeof(self) weakSelf = self;
    
    //切换报警弹框内容事假处理
    self.alarmViewBg.exchangeAlarmBlock = ^(NSInteger tag){
        [weakSelf changeAlarmTypeWithTag:tag];
    };
    
}
//切换报警弹框内容
- (void)changeAlarmTypeWithTag:(NSInteger )tag {
    
    NSInteger index = tag - 200;
    self.iolInfoListView.isCar = YES;
    if (index == globalNum0) {
        self.iolInfoListView.type = index;
    }else{
        self.iolInfoListView.type = index+1;
    }
    self.iolInfoListView.startTime = self.startTime;
    self.iolInfoListView.endTime = self.endTime;
    self.iolInfoListView.carShopId = self.carShopId;
    if ([self.iolInfoListView isShow]) {
        [self.iolInfoListView hidden];
    }else{
        [self.view.superview addSubview:self.iolInfoListView];
        [self.iolInfoListView show];
    }
}

- (void)setUpVehicleAmountAndMileageView {
    
    
    __weak typeof(self) weakSelf = self;
    
    self.middleViewBg.workAreaDetailBlock = ^(){
        
        CLWorkAreaDetailViewController *workAreaDetailVC = [[CLWorkAreaDetailViewController alloc] init];
        [weakSelf.navigationController pushViewController:workAreaDetailVC animated:YES];
        workAreaDetailVC.carShopId = weakSelf.carShopId;
        
        weakSelf.seleetdCarShopModel = [[CarShopListModel alloc] init];
        
        if (nil == weakSelf.seleetdCarShopModel.name) {
            weakSelf.seleetdCarShopModel.name = kDefaultWorkShopName;
        }
        
        if (nil == weakSelf.seleetdCarShopModel.carShopId) {
            weakSelf.seleetdCarShopModel.carShopId = kDefalutCarShopId;
        }
        workAreaDetailVC.seleetdCarShopModel = weakSelf.seleetdCarShopModel;
    };
    
    self.middleViewBg.exchangeVehicleBlock = ^(NSInteger index){
        //进入车辆实时状态页面
        FCVehicleReamTimeStatusController *vehicleReamTimeStatusVC = [[FCVehicleReamTimeStatusController alloc] init];
        vehicleReamTimeStatusVC.type = index;
        vehicleReamTimeStatusVC.carModel = self.homeCarModel;
        [weakSelf.navigationController pushViewController:vehicleReamTimeStatusVC animated:YES];
    };
}

- (void)setUpChooseWorkAreaBoxView {
    
    __weak typeof(self) weakSelf = self;
    
    self.chooseBoxBg.dataArray = [NSMutableArray arrayWithArray:self.carShopListArray];
    self.chooseBoxBg.clearViewframe = self.view.frame;
    //选择工作区操作
    self.chooseBoxBg.chooseBoxClickBlock = ^(id chooseResult){
        
        weakSelf.seleetdCarShopModel = (CarShopListModel *)chooseResult;
        weakSelf.carShopId = weakSelf.seleetdCarShopModel.carShopId;
        
        [weakSelf.homeCarParamsDic setObject:weakSelf.carShopId forKey:@"workShopId"];
        [weakSelf getHomeCarInfoListWith:weakSelf.homeCarParamsDic];
    };
    
    
}

- (void)setUpNetWorkData {
    
    [self.middleViewBg setObjectWith:self.homeCarModel];
    [self.alarmViewBg setObjectWith:self.homeCarModel];
}
#pragma mark - 网络请求
- (void)getHomeCarInfoListWith:(NSString *)startTime andEndTime:(NSString *)endTime {

    [self.homeCarParamsDic setObject:self.startTime forKey:@"starttime"];
    [self.homeCarParamsDic setObject:self.endTime forKey:@"endtime"];
    
    [self getHomeCarInfoListWith:self.homeCarParamsDic];

}
- (void)getHomeCarInfoListWith:(NSMutableDictionary *)dic {
    __weak typeof(self) weakSelf = self;
    
    [HomeCarModel getHomeCarInfoList:dic success:^(NSDictionary *response) {
        NSLog(@"getHomeCarInfoList_____%@response",response);
        weakSelf.homeCarModel = [HomeCarModel mj_objectWithKeyValues:response];
        [weakSelf setUpNetWorkData];
    }];
}

- (void)getShopCarListInfoWith:(NSMutableDictionary *)dic {
    __weak typeof(self) weakSelf = self;
    
    [CarShopListModel getcarShopListInfo:dic success:^(NSDictionary *response) {
        weakSelf.carShopListArray = [NSMutableArray array];
        
        NSArray *carShopList = response[@"carShopList"];
        
        for (NSDictionary *model in carShopList) {
            CarShopListModel *carShopListModel = [CarShopListModel mj_objectWithKeyValues:model];
            [weakSelf.carShopListArray addObject:carShopListModel];
        }
        
        [weakSelf setUpChooseWorkAreaBoxView];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([self.iolInfoListView isShow]) {
        self.iolInfoListView.maskView.hidden = NO;
    }
}

@end
